package com.codeavatar.catools.helper;

import android.content.Context;
import android.util.TypedValue;

import com.codeavatar.catools.R;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class CaValuesHelper {

    private final String TAG = this.getClass().getSimpleName();

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++

    public float dpToPxByTypeValue(Context context, float dp) {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

    public float spToPxByTypeValue(Context context, float sp) {
        return TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
    }

    public int dpToPx(Context context, int dip) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
    }

    public int pxToDp(Context context, int px) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f * (px >= 0 ? 1 : -1));
    }

    public int spToPx(Context context, float spValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public int pxToSp(Context context, float pxValue) {
        float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    public int getDimenOneDp(Context context) {
        float f = context.getResources().getDimension(R.dimen.ca_one_dp);
        return (int) f;
    }

    public int getDimenOneSp(Context context) {
        float f = context.getResources().getDimension(R.dimen.ca_one_sp);
        return (int) f;
    }

    public int getDimenVal(Context context, int dimenResId) {
        float f = context.getResources().getDimension(dimenResId);
        return (int) f;
    }

    public String getStringVal(Context context, int stringResId) {
        return context.getResources().getString(stringResId);
    }

    public int getColorVal(Context context, int colorResId) {
        return context.getResources().getColor(colorResId);
    }

    private static CaValuesHelper init;
    public static CaValuesHelper init(){
        if(null == init){
            init = new CaValuesHelper();
        }
        return init;
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private CaValuesHelper(){
        // Private constructor to prevent unwanted construction.
    }
}
